package cn.wx.pet.controller;


import cn.wx.basic.annotation.PreAuthorize;
import cn.wx.basic.util.GetConTest;
import cn.wx.basic.util.JsonResult;
import cn.wx.basic.util.PageList;

import cn.wx.pet.domain.Pet;
import cn.wx.pet.query.PetQuery;
import cn.wx.pet.service.IPetService;
import cn.wx.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class PetController {

    @Autowired
    private IPetService iPetService;

    //使用get方式请求  查询所有
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Pet> findAll(HttpServletRequest request){

        Logininfo logininfo = GetConTest.getLogininfo(request);
        return iPetService.loadAll();
    }

    @GetMapping("/{id}")   //获取get请求里面的参数   查询一个
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "单独查询宠物",sn = "pet:get") //value后面用来做权限校验
    public Pet findById(@PathVariable("id") Long id ){
        return iPetService.loadById(id);
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "删除宠物",sn = "pet:delete") //value后面用来做权限校验
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            iPetService.remove(id);
           return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //添加和更新     //使用put方式请求
    @PutMapping
    @ApiOperation(value = "增加修改" )
    public JsonResult save(@RequestBody Pet pet){
        try {
            if(pet.getId()==null){
                iPetService.save(pet);
            }else{
                iPetService.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    //分页
    @PostMapping      //使用post方式请求
    @ApiOperation(value = "分页查询" )
    public PageList<Pet> queryPage(@RequestBody PetQuery query){

        try {
            System.out.println(query);
            return  iPetService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    //批量删除
    @PatchMapping
    public JsonResult  patchDel(@RequestBody List<Long> ids){
        try {
            iPetService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }


    //子集部门
/*
    @GetMapping("/deptTree")
    public List<Pet> deptTree(){
       return iPetService.deptTree();
    }*/

    /**
     * 下架
     * @param
     * @return
     */
    @PostMapping("/offsale")
    @ApiOperation(value = "下架" )
    public JsonResult offSale(@RequestBody List<Long> ids){

        try {
            iPetService.offSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }

    /**
     * 上架
     * @param
     * @return
     */
    @PostMapping("/onsale")
    @ApiOperation(value = "上架")
    public JsonResult onSale(@RequestBody List<Long> ids){

        try {
            iPetService.onSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟，稍后重试");
        }
    }



}
